package com.fsserver.web.controller;

import com.fsserver.common.api.ApiResponse;
import com.fsserver.core.dto.ReconciliationRequestDTO;
import com.fsserver.core.entity.Reconciliation;
import com.fsserver.core.service.ReconciliationService;
import com.fsserver.core.vo.ReconciliationResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 对账控制器
 * Reconciliation Controller
 *
 * @author fsserver
 */
@Slf4j
@RestController
@RequestMapping("/reconciliation")
@Api(tags = "对账管理接口", description = "提供对账创建、查询、执行等功能")
public class ReconciliationController {

    @Autowired
    private ReconciliationService reconciliationService;

    /**
     * 创建对账
     * Create reconciliation
     *
     * @param reconciliationRequestDTO 对账请求DTO
     * @return API响应
     */
    @PostMapping
    @ApiOperation(value = "创建对账", notes = "根据对账请求DTO创建对账")
    public ApiResponse<ReconciliationResponseVO> createReconciliation(@Valid @RequestBody ReconciliationRequestDTO reconciliationRequestDTO) {
        ReconciliationResponseVO reconciliationResponseVO = reconciliationService.createReconciliation(reconciliationRequestDTO);
        return ApiResponse.success(reconciliationResponseVO);
    }

    /**
     * 查询对账
     * Query reconciliation
     *
     * @param reconciliationNo 对账号
     * @return API响应
     */
    @GetMapping("/{reconciliationNo}")
    @ApiOperation(value = "查询对账", notes = "根据对账号查询对账")
    @ApiImplicitParam(name = "reconciliationNo", value = "对账号", required = true, dataType = "String", paramType = "path")
    public ApiResponse<Reconciliation> queryReconciliation(@PathVariable String reconciliationNo) {
        Reconciliation reconciliation = reconciliationService.queryReconciliation(reconciliationNo);
        return ApiResponse.success(reconciliation);
    }

    /**
     * 根据支付类型查询对账列表
     * List reconciliations by payment type
     *
     * @param paymentType 支付类型
     * @return API响应
     */
    @GetMapping("/payment-type/{paymentType}")
    @ApiOperation(value = "根据支付类型查询对账列表", notes = "根据支付类型查询对账列表")
    @ApiImplicitParam(name = "paymentType", value = "支付类型", required = true, dataType = "String", paramType = "path")
    public ApiResponse<List<Reconciliation>> listReconciliationsByPaymentType(@PathVariable String paymentType) {
        List<Reconciliation> reconciliations = reconciliationService.listReconciliationsByPaymentType(paymentType);
        return ApiResponse.success(reconciliations);
    }

    /**
     * 根据日期范围查询对账列表
     * List reconciliations by date range
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return API响应
     */
    @GetMapping("/date-range")
    @ApiOperation(value = "根据日期范围查询对账列表", notes = "根据日期范围查询对账列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "startDate", value = "开始日期", required = true, dataType = "String", paramType = "query", example = "2023-01-01"),
        @ApiImplicitParam(name = "endDate", value = "结束日期", required = true, dataType = "String", paramType = "query", example = "2023-12-31")
    })
    public ApiResponse<List<Reconciliation>> listReconciliationsByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        List<Reconciliation> reconciliations = reconciliationService.listReconciliationsByDateRange(startDate, endDate);
        return ApiResponse.success(reconciliations);
    }

    /**
     * 执行对账
     * Execute reconciliation
     *
     * @param reconciliationNo 对账号
     * @return API响应
     */
    @PostMapping("/execute/{reconciliationNo}")
    @ApiOperation(value = "执行对账", notes = "根据对账号执行对账")
    @ApiImplicitParam(name = "reconciliationNo", value = "对账号", required = true, dataType = "String", paramType = "path")
    public ApiResponse<Boolean> executeReconciliation(@PathVariable String reconciliationNo) {
        boolean result = reconciliationService.executeReconciliation(reconciliationNo);
        return ApiResponse.success(result);
    }
} 